//
//  ContactManManager.m
//  QuanZiChat
//
//  Created by lanou3g on 15/11/25.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "ContactManManager.h"
#import "ContactInfo.h"
#import "MessageFrame.h"
#import "MessageInfo.h"
#import <AVObject.h>
#import <AVQuery.h>
#import <AVOSCloud.h>
#import <AVOSCloudIM.h>

@interface ContactManManager()
//对话发起者
@property(nonatomic,strong)AVIMClient *server;
//保存对话信息的数组
@property(nonatomic,strong)NSMutableArray *talkArray;

@end

@implementation ContactManManager

//单例
static ContactManManager *Manager = nil;
+(instancetype)defaulManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Manager = [[ContactManManager alloc] init];
    });
    return Manager;
}

//初始化
-(instancetype)init{
    self = [super init];
    if (self) {
        self.server = [[AVIMClient alloc] init];
    }
    return self;
}


//懒加载ContactArray
-(NSMutableArray *)contactArray{
    if(_contactArray  == nil){
        _contactArray = [NSMutableArray array];
    }
    return _contactArray;
}


//懒加载talkArray
-(NSMutableArray *)talkArray{
    if(_talkArray == nil){
        _talkArray = [NSMutableArray array];
    }
    return _talkArray;
}


#pragma mark =========== LeanCloud数据库操作
//从服务器数据库中导出所有联系人
-(void)getAllPerson:(void(^)(NSMutableArray *result)) callback{
    [AVQuery doCloudQueryInBackgroundWithCQL:@"select * from _User"  pvalues:nil callback:^(AVCloudQueryResult *result, NSError *error) {
        NSArray *Array = result.results;
        for (AVUser *user in Array) {
            ContactInfo *contact = [[ContactInfo alloc] init];
            contact.name = user.username;
            contact.UserID = user.objectId;
            [self.contactArray addObject:contact];
        }
        //block回调
        callback(self.contactArray);
    }];
}

//发送消息
-(void)sendMessageWithMyClientID:(NSString *)myClientID OtherClientID:(NSString *)otherClientID Message:(NSString *)message{
    [self.server openWithClientId:myClientID callback:^(BOOL succeeded, NSError *error) {
        [self.server createConversationWithName:@"会话" clientIds:@[otherClientID] callback:^(AVIMConversation *conversation, NSError *error) {
            [conversation sendMessage:[AVIMTextMessage messageWithText:message attributes:nil] callback:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"发送成功");
                }
            }];
        }];
    }];
}


//接收消息的方法
-(void)recriveMessageWithDelegate:(id)mydelegate OtherClient:(NSString *)otherclient{
    self.server.delegate = mydelegate;
    [self.server openWithClientId:otherclient callback:^(BOOL succeeded, NSError *error) {
        NSLog(@"%@",error);
    }];
}

//建立发送模型(以这个格式来发送或接受消息)
-(void)buildSendModelWithString:(NSString *)str type:(NSString *)type{
    MessageInfo *messageinfo = [[MessageInfo alloc] init];
    //获取上一条信息
    MessageFrame *lastmessageframe = [self.talkArray lastObject];
    //判断当前信息和上一条的发送时间是否相同,如果相同则隐藏"时间Lable"
    if([messageinfo.time isEqualToString:lastmessageframe.messageinfo.time]){
        messageinfo.hideTime = YES;
    }
    //判断发送信息者是自己还是对象
    if([type isEqualToString:@"1"]){
        messageinfo.type = MessageMe;
    }else{
        messageinfo.type = MessageYou;
    }
    messageinfo.text = str;
    //创建一个frame模型
    MessageFrame *model = [[MessageFrame alloc] init];
    model.messageinfo = messageinfo;
    //将对话模型添加到聊天数组中
    [self.talkArray addObject:model];
}

//返回聊天数组(数组包含和所有用户的聊天内容)
-(NSMutableArray *)getTalkArray{
    return self.talkArray;
}
@end
